﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XRpgLibrary.TileEngine;
using EyesOfTheDragon.Events;
using XRpgLibrary.Events;

namespace XRpgLibrary.SpriteClasses
{
    public class AnimatedSprite :IDraw,IPhysics
    {
        #region Field Region
        public Dictionary<AnimationKey, Animation> animations;
        AnimationKey currentAnimation;
        bool isAnimating = true;
        public Texture2D texture;        
        public Vector2 position;
        Vector2 velocity;
        float speed = 2.0f;
        public bool Visible = true;
        private Vector2 _scale = new Vector2(1, 1);
       
        #endregion
        #region Property Region
        public Vector2 Scale
        {
            get { return _scale; }
            set { _scale = value; }
        }
        public AnimationKey CurrentAnimation
        {
            get { return currentAnimation; }
            set { currentAnimation = value; }
        }
        public bool IsAnimating
        {
            get { return isAnimating; }
            set { isAnimating = value; }
        }
        public int Width
        {
            get { return animations[currentAnimation].FrameWidth; }
        }
        public int Height
        {
            get { return animations[currentAnimation].FrameHeight; }
        }
        public float Speed
        {
            get { return speed; }
            set { speed = MathHelper.Clamp(speed, 1.0f, 16.0f); }
        }
        public Vector2 Position
        {
            get { return position; }
            set
            {
                position = value;
            }
        }
        public Vector2 Velocity
        {
            get { return velocity; }
            set
            {
                velocity = value;
            }
        }
        #endregion
        #region Constructor Region
        public AnimatedSprite(Texture2D sprite, Dictionary<AnimationKey, Animation> animation)
        {
            texture = sprite;
            animations = animation;
           // foreach (AnimationKey key in animation.Keys)
          //      animations.Add(key, (Animation)animation[key].Clone());
        }       
        #endregion
        #region Method Region
        public Rectangle getRect()
        {
            return new Rectangle((int)position.X, (int)position.Y, (int)(Width * _scale.X), (int)(Height * _scale.Y));
        }
        public void Update(GameTime gameTime)
        {
            if (isAnimating)
                animations[currentAnimation].Update(gameTime);
        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (!Visible)
                return;
            Texture2D tx;
            if (animations[currentAnimation].Texture != null)
                tx = animations[currentAnimation].Texture;
            else
                tx = texture;
          if (Scale == new Vector2(1, 1))
            {
                spriteBatch.Draw(
                    tx,
                    position,
                    animations[currentAnimation].CurrentFrameRect,
                    Color.White);
            }
            else
               spriteBatch.Draw(tx, new Rectangle((int)position.X, (int)position.Y, (int)(Width * _scale.X), (int)(Height * _scale.Y)), animations[CurrentAnimation].CurrentFrameRect, Color.White);
           
        }
        public void LockToMap(float mapWidth, float mapHeight)
        {
            position.X = MathHelper.Clamp(position.X, 0, mapWidth);
            position.Y = MathHelper.Clamp(position.Y, 0, mapHeight);
        }
        #endregion
    }
}